package com.zsc.wxm.ysh.anjo.house.controller;

import com.zsc.wxm.ysh.anjo.house.service.BaseDataService;
import com.zsc.wxm.ysh.anjo.result.PageResult;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.dto.BaseDataDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.qo.BaseDataQuery;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.vo.BaseDataVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/12 23:03
 */
@RestController
@RequestMapping("/base-data")
public class BaseDataController {

    @Resource
    private BaseDataService baseDataService;

    @PostMapping("/add-base-data")
    @ApiOperation("增加基础数据")
    public Result addBaseData(@Validated @RequestBody BaseDataDTO params) {
        baseDataService.addBaseData(params);
        return Result.ok("添加成功");
    }

    @PutMapping("/update")
    @ApiOperation("更新数据")
    public Result update(@Validated @RequestBody BaseDataDTO params) {
        baseDataService.updateBaseData(params);
        return Result.ok("修改成功");
    }

    @PostMapping("/list-page")
    @ApiOperation("根据条件查询")
    public PageResult listPage(@RequestBody BaseDataQuery params) {
        return baseDataService.listPage(params);
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation("删除记录")
    public Result delete(@PathVariable("id") Long id) {
        baseDataService.delete(id);
        return Result.ok("删除成功");
    }

    @GetMapping("/list/{type}")
    @ApiOperation("根据类型查询")
    public Result list(@PathVariable("type") String type) {
        List<BaseDataVO> list = baseDataService.listByType(type);
        Map<String, Object> data = new HashMap<>();
        data.put("data", list);
        return Result.ok("获取成功", data);
    }

    @GetMapping("/get-by-id/{id}")
    @ApiOperation("根据id查询")
    public Result getById(@PathVariable("id") Long id) {
        Map<String, Object> data = new HashMap<>();
        data.put("data", baseDataService.getOneById(id));
        return Result.ok("获取成功", data);
    }
}
